/**
 * Copyright © 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.wutu.nbyjk8.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.wutu.nbyjk8.domain.CarPassThroughRecord;
import com.wutu.nbyjk8.domain.CarPassThroughRecordDTO;
import com.wutu.nbyjk8.service.impl.CarPassThroughRecordHandler;
import com.wutu.nbyjk8.service.impl.CarPassThroughRecordService;
import com.wutu.nbyjk8.util.DateUtils;
import com.wutu.nbyjk8.vo.QueryVO;
import com.wutu.nbyjk8.vo.TimeGroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 车辆通行记录Controller
 *
 * @author 朱欢乐
 * @version 2025-09-25
 */

@Api(tags = "车辆通行记录")
@RestController
@RequestMapping(value = "/car/passrecord/carPassThroughRecord")
public class CarPassThroughRecordController {

    @Autowired
    private CarPassThroughRecordService carPassThroughRecordService;


    /**
     * 车辆通行记录列表数据
     */
    @ApiOperation(value = "查询车辆通行记录列表数据")
    @GetMapping("list")
    public ResponseEntity<IPage<CarPassThroughRecord>> list(CarPassThroughRecordDTO carPassThroughRecordDTO, Page<CarPassThroughRecord> page) throws Exception {
        QueryWrapper<CarPassThroughRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("inserttime");
        IPage<CarPassThroughRecord> result = carPassThroughRecordService.page(page, queryWrapper);
        return ResponseEntity.ok(result);
    }


    @ApiOperation(value = "按小时分组")
    @GetMapping("hour")
    public ResponseEntity<List<TimeGroupVO>> hour(QueryVO queryVO) throws ParseException {
        QueryWrapper<CarPassThroughRecord> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        final String format = simpleDateFormat.format(new Date());
        String startTime1 = format + " 00:00:00";
        String endTime1 = format + " 23:59:59";
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date startTime = simpleDateFormat1.parse(startTime1);
        final Date endTime = simpleDateFormat1.parse(endTime1);

        queryWrapper.nested(wrapper -> {
            wrapper.ge("itime", startTime).le("itime", endTime);
            wrapper.or().ge("otime", startTime).le("otime", endTime);
        });
        final List<CarPassThroughRecord> list = carPassThroughRecordService.list(queryWrapper);
        return ResponseEntity.ok(CarPassThroughRecordHandler.groupByHour(list));
    }


    @ApiOperation(value = "按日期分组")
    @GetMapping("day")
    public ResponseEntity<List<TimeGroupVO>> day(QueryVO queryVO) {
        QueryWrapper<CarPassThroughRecord> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = simpleDateFormat.format(queryVO.getStartTime());
        String endTime = simpleDateFormat.format(queryVO.getEndTime());
        queryWrapper.nested(wrapper -> {
            wrapper.ge("itime", queryVO.getStartTime()).le("itime", queryVO.getEndTime());
            wrapper.or().ge("otime", queryVO.getStartTime()).le("otime", queryVO.getEndTime());
        });
        final List<CarPassThroughRecord> list = carPassThroughRecordService.list(queryWrapper);
        return ResponseEntity.ok(CarPassThroughRecordHandler.groupByDate(list, startTime.substring(0, 10), endTime.substring(0, 10)));
    }


    @ApiOperation(value = "按收入日期分组")
    @GetMapping("/sr/week")
    public ResponseEntity<List<TimeGroupVO>> srweek(QueryVO queryVO) throws ParseException {
        QueryWrapper<CarPassThroughRecord> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        queryVO.setStartTime(simpleDateFormat.parse(DateUtils.getStartOfWeek() + " 00:00:00"));
        queryVO.setEndTime(simpleDateFormat.parse(DateUtils.getEndOfWeek() + " 23:59:59"));
        queryWrapper.eq("iotype", "2");
        queryWrapper.nested(wrapper -> {
            wrapper.ge("itime", queryVO.getStartTime()).le("itime", queryVO.getEndTime());
            wrapper.or().ge("otime", queryVO.getStartTime()).le("otime", queryVO.getEndTime());
        });
        final List<CarPassThroughRecord> list = carPassThroughRecordService.list(queryWrapper);
        return ResponseEntity.ok(CarPassThroughRecordHandler.groupByDay(list, queryVO.getStartTime(), queryVO.getEndTime()));
    }


    @ApiOperation(value = "按收入日期分组")
    @GetMapping("/sr/month")
    public ResponseEntity<List<TimeGroupVO>> srmonth(QueryVO queryVO) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        queryVO.setStartTime(simpleDateFormat.parse(DateUtils.getStartOfQuarter() + " 00:00:00"));
        queryVO.setEndTime(simpleDateFormat.parse(DateUtils.getEndOfQuarter() + " 23:59:59"));
        QueryWrapper<CarPassThroughRecord> queryWrapper = new QueryWrapper<>();
        String startTime = simpleDateFormat.format(queryVO.getStartTime());
        String endTime = simpleDateFormat.format(queryVO.getEndTime());
        queryWrapper.eq("iotype", "2");
        queryWrapper.nested(wrapper -> {
            wrapper.ge("itime", queryVO.getStartTime()).le("itime", queryVO.getEndTime());
            wrapper.or().ge("otime", queryVO.getStartTime()).le("otime", queryVO.getEndTime());
        });
        final List<CarPassThroughRecord> list = carPassThroughRecordService.list(queryWrapper);
        return ResponseEntity.ok(CarPassThroughRecordHandler.groupByMonth(list, queryVO.getStartTime(), queryVO.getEndTime()));
    }

    @ApiOperation(value = "今日停车时长分组")
    @GetMapping("sctype")
    public ResponseEntity<List<TimeGroupVO>> sctype() throws ParseException {
        QueryWrapper<CarPassThroughRecord> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        final String format = simpleDateFormat.format(new Date());
        String startTime1 = format + " 00:00:00";
        String endTime1 = format + " 23:59:59";
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date startTime = simpleDateFormat1.parse(startTime1);
        final Date endTime = simpleDateFormat1.parse(endTime1);


        queryWrapper.eq("iotype", "2");
        queryWrapper.nested(wrapper -> {
            wrapper.ge("itime", startTime).le("itime", endTime);
            wrapper.or().ge("otime", startTime).le("otime", endTime);
        });
        final List<CarPassThroughRecord> list = carPassThroughRecordService.list(queryWrapper);
        Integer d1 = 0;
        Integer d2 = 0;
        Integer d3 = 0;
        List<TimeGroupVO> list1 = new ArrayList<>();
        for (CarPassThroughRecord carPassThroughRecord : list) {
            if (carPassThroughRecord.getPtime() < 1) {
                d1 += 1;
            } else if (carPassThroughRecord.getPtime() < 4) {
                d2 += 1;
            } else {
                d3 += 1;
            }
        }
        TimeGroupVO timeGroupVO1 = new TimeGroupVO();
        timeGroupVO1.setType("1h");
        timeGroupVO1.setNum(d1);
        list1.add(timeGroupVO1);
        TimeGroupVO timeGroupVO2 = new TimeGroupVO();
        timeGroupVO2.setType("1h~4h");
        timeGroupVO2.setNum(d2);
        list1.add(timeGroupVO2);
        TimeGroupVO timeGroupVO3 = new TimeGroupVO();
        timeGroupVO3.setType("4h~");
        timeGroupVO3.setNum(d3);
        list1.add(timeGroupVO3);
        return ResponseEntity.ok(list1);
    }


    @ApiOperation(value = "今日停车类型分组")
    @GetMapping("lxtype")
    public ResponseEntity<List<TimeGroupVO>> lxtype() throws ParseException {
        QueryWrapper<CarPassThroughRecord> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        final String format = simpleDateFormat.format(new Date());
        String startTime1 = format + " 00:00:00";
        String endTime1 = format + " 23:59:59";
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date startTime = simpleDateFormat1.parse(startTime1);
        final Date endTime = simpleDateFormat1.parse(endTime1);
        //queryWrapper.eq("iotype", 2);
        queryWrapper.nested(wrapper -> {
            wrapper.ge("itime", startTime).le("itime", endTime);
            wrapper.or().ge("otime", startTime).le("otime", endTime);
        });
        final List<CarPassThroughRecord> list = carPassThroughRecordService.list(queryWrapper);
        Integer d1 = 0;
        Integer d2 = 0;
        List<TimeGroupVO> list1 = new ArrayList<>();
        for (CarPassThroughRecord carPassThroughRecord : list) {
            if ("0".equals(carPassThroughRecord.getType())) {
                d1 += 1;
            } else {
                d2 += 1;
            }
        }
        TimeGroupVO timeGroupVO1 = new TimeGroupVO();
        timeGroupVO1.setType("临时车辆");
        timeGroupVO1.setNum(d1);
        list1.add(timeGroupVO1);
        TimeGroupVO timeGroupVO2 = new TimeGroupVO();
        timeGroupVO2.setType("固定车辆");
        timeGroupVO2.setNum(d2);
        list1.add(timeGroupVO2);
        return ResponseEntity.ok(list1);
    }


    /**
     * 根据Id获取车辆通行记录数据
     */
    @ApiOperation(value = "根据Id获取车辆通行记录数据")
    @GetMapping("queryById")
    public ResponseEntity<CarPassThroughRecord> queryById(String id) {
        return ResponseEntity.ok(carPassThroughRecordService.getById(id));
    }

    /**
     * 保存车辆通行记录
     */
    @ApiOperation(value = "保存车辆通行记录")
    @PostMapping("save")
    public ResponseEntity<String> save(@RequestBody CarPassThroughRecord carPassThroughRecord) {
        //新增或编辑表单保存
        carPassThroughRecordService.saveOrUpdate(carPassThroughRecord);
        return ResponseEntity.ok("保存车辆通行记录成功");
    }


    /**
     * 删除车辆通行记录
     */
    @ApiOperation(value = "删除车辆通行记录")
    @DeleteMapping("delete")
    public ResponseEntity<String> delete(String ids) {
        String idArray[] = ids.split(",");
        carPassThroughRecordService.removeByIds(Lists.newArrayList(idArray));
        return ResponseEntity.ok("删除车辆通行记录成功");
    }
}
